﻿using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;

//自定义命名空间
using DreamCube.Foundation.Basic.Utility;

namespace AutoCompile.UI
{
    /// <summary>
    /// 操作配置文件的类
    /// </summary>
    public static class ConfigsHelper
    {
        private static String configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data.xml");

        private static XmlDocument doc = null;

        public static XmlDocument GetXmlDoc()
        {
            if(doc != null)return doc;
            doc = new XmlDocument();
            if (File.Exists(configFilePath)) doc.Load(configFilePath);
            //else
            //{
            //    XmlElement rootNode = doc.CreateElement("Configs");
            //    rootNode.SetAttribute("DefaultConfig", "");
            //    doc.AppendChild(rootNode);
            //    doc.Save(configFilePath);
            //}
            return doc;
        }

        /// <summary>
        /// 添加xml节点
        /// </summary>
        /// <param name="configName"></param>
        /// <param name="projectName"></param>
        /// <param name="projectPath"></param>
        /// <param name="frameworkVersion"></param>
        /// <param name="outputPath"></param>
        public static void ConfigNode(String configName, List<ProjectElement> projects)
        {
            XmlDocument doc = GetXmlDoc();
            XmlNode configNode = doc.SelectSingleNode(String.Format("/Configs/Config[@Name='{0}']", configName));
            XmlNode projectsNode = null;
            if (configNode != null)  //存在指定名称的配置，则更新该配置节点即可
            {
                 projectsNode = configNode.SelectSingleNode("./Projects");
                 if (projectsNode == null)
                     projectsNode = doc.CreateElement("Projects");
            }
            else  //新增配置
            {
                XmlNode rootNode = doc.SelectSingleNode("/Configs");
                if (rootNode == null) //如果是新文档，则根节点还不存在
                {
                    rootNode = doc.CreateElement("Configs");
                    ((XmlElement)rootNode).SetAttribute("DefaultConfig", configName);
                }
                else
                {
                    rootNode.Attributes["DefaultConfig"].Value = configName;
                }
                doc.AppendChild(rootNode);

                XmlElement config = doc.CreateElement("Config");
                config.SetAttribute("Name", configName);
                rootNode.AppendChild(config);
                projectsNode = doc.CreateElement("Projects");
                config.AppendChild(projectsNode);
            }
            XmlNodeList projectNodeList = projectsNode.SelectNodes("./Project");
            if (projectNodeList != null && projectNodeList.Count > 0)
            {
                foreach (XmlNode tempNode in projectNodeList)
                {
                    projectsNode.RemoveChild(tempNode);
                }
            }
            if (projects != null)
            {
                //假如选中的配置文件
                for (Int32 i = 0; i < projects.Count; i++)
                {
                    XmlElement projectElement = doc.CreateElement("Project");
                    projectElement.SetAttribute("ProjectName", projects[i].ProjectName);
                    projectElement.SetAttribute("ProjectPath", projects[i].ProjectPath);
                    projectElement.SetAttribute("FrameworkVersion", projects[i].FrameworkVersion);
                    projectElement.SetAttribute("OutputPath", projects[i].OutputPath);
                    projectElement.SetAttribute("Config", projects[i].Config);
                    projectElement.SetAttribute("Platform", projects[i].Platform);
                    projectsNode.AppendChild(projectElement);
                }
            }

            doc.Save(configFilePath);
        }

        /// <summary>
        /// 获取指定的配置的所有项目
        /// </summary>
        /// <param name="configName"></param>
        /// <returns></returns>
        public static List<ProjectElement> GetConfigProjects(String configName)
        {
            XmlDocument doc = GetXmlDoc();
            XmlNode configNode = doc.SelectSingleNode(String.Format("/Configs/Config[@Name='{0}']", configName));
            List<ProjectElement> projectElements = new List<ProjectElement>();
            if (configNode != null)
            {
                XmlNodeList nodeList = configNode.SelectNodes("./Projects/Project");
                foreach (XmlNode node in nodeList)
                {
                    ProjectElement pj = new ProjectElement();
                    MyObject.LoadPropertyValueFromXmlNode(node,pj);
                    projectElements.Add(pj);
                }
            }
            return projectElements;
        }
        
        /// <summary>
        /// 获取所有的配置名称
        /// </summary>
        /// <returns></returns>
        public static List<String> GetAllConfigNames()
        {
            XmlDocument doc = GetXmlDoc();
            XmlNodeList nodes = doc.SelectNodes("/Configs/Config");
            List<String> configNames = new List<String>();
            if (nodes != null && nodes.Count > 0)
            {
                for (Int32 i = 0; i < nodes.Count; i++)
                    configNames.Add(nodes[i].Attributes["Name"].InnerText);
            }
            return configNames;
        }

        /// <summary>
        /// 更新配置名称
        /// </summary>
        /// <param name="oldName"></param>
        /// <param name="newName"></param>
        public static void UpdateConfigName(String oldName, String newName)
        {
            XmlDocument doc = GetXmlDoc();
            XmlNode configNode = doc.SelectSingleNode(String.Format("/Configs/Config[@Name='{0}']", oldName));
            configNode.Attributes["Name"].InnerText = newName;
            doc.Save(configFilePath);
        }

        /// <summary>
        /// 根据名称移除指定配置
        /// </summary>
        /// <param name="configName"></param>
        public static void RemoveConfig(String configName)
        {
            XmlDocument doc = GetXmlDoc();
            XmlNode configNode = doc.SelectSingleNode(String.Format("/Configs/Config[@Name='{0}']", configName));
            configNode.ParentNode.RemoveChild(configNode);
            //doc.RemoveChild(configNode);
            doc.Save(configFilePath);
        }

        /// <summary>
        /// 获取默认的配置名称
        /// </summary>
        /// <returns></returns>
        public static String GetDefaultConfigName()
        {
            XmlDocument doc = GetXmlDoc();
            XmlNode node = doc.SelectSingleNode("/Configs");
            if (node != null)
                return node.Attributes["DefaultConfig"].InnerText;
            return String.Empty;
        }

        /// <summary>
        /// 获取输出目录
        /// </summary>
        /// <returns></returns>
        public static String GetOutputPath()
        {
            XmlNode node = doc.SelectSingleNode("/Configs");
            if (node != null)
            {
                XmlAttribute attr = node.Attributes["OutputPath"];
                if (attr != null) return attr.InnerText;
            }
            return String.Empty;
        }

        /// <summary>
        /// 获取配置的Devenv的路径
        /// </summary>
        /// <returns></returns>
        public static String GetDevenvPath()
        {
            XmlNode node = doc.SelectSingleNode("/Configs");
            if (node != null)
            {
                XmlAttribute attr = node.Attributes["OutputPath"];
                if (attr != null) return attr.InnerText;
            }
            return String.Empty;
        }

        /// <summary>
        /// 获取保存的是否每次都更新代码的操作
        /// </summary>
        /// <returns></returns>
        public static Boolean GetUpdateCodeOperation()
        {
            XmlNode node = doc.SelectSingleNode("/Configs");
            if (node != null)
            {
                XmlAttribute attr = node.Attributes["UpdateCodeEveryTime"];
                if (attr != null) return Boolean.Parse(attr.InnerText);
            }
            return false;
        }

        /// <summary>
        /// 保存输出目录
        /// </summary>
        /// <param name="newPath"></param>
        public static void SaveOutputPath(String newPath)
        {
            XmlDocument doc = GetXmlDoc();
            XmlNode node = doc.SelectSingleNode("/Configs");
            if (node != null)
            {
                XmlAttribute attr = node.Attributes["OutputPath"];
                if (attr == null) attr = doc.CreateAttribute("OutputPath");
                attr.InnerText = newPath;
                node.Attributes.Append(attr);
                doc.Save(configFilePath);
            }
        }

        /// <summary>
        /// 保存devenv.exe的目录
        /// </summary>
        /// <param name="newPath"></param>
        public static void SaveDevenvPath(String newPath)
        {
            XmlDocument doc = GetXmlDoc();
            XmlNode node = doc.SelectSingleNode("/Configs");
            if (node != null)
            {
                XmlAttribute attr = node.Attributes["DevenvPath"];
                if (attr == null) attr = doc.CreateAttribute("DevenvPath");
                attr.InnerText = newPath;
                node.Attributes.Append(attr);
                doc.Save(configFilePath);
            }
        }

        /// <summary>
        /// 保存是否每次都更新代码的操作的配置
        /// </summary>
        /// <param name="value"></param>
        public static void SaveSourceCodeType(String value)
        {
            XmlDocument doc = GetXmlDoc();
            XmlNode node = doc.SelectSingleNode("/Configs");
            if (node != null)
            {
                XmlAttribute attr = node.Attributes["SourceCodeType"];
                if (attr == null) attr = doc.CreateAttribute("SourceCodeType");
                attr.InnerText = value;
                node.Attributes.Append(attr);
                doc.Save(configFilePath);
            }
        }

        /// <summary>
        /// 保存是否每次都更新代码的操作的配置
        /// </summary>
        /// <param name="value"></param>
        public static void SaveUpdateCodeOperation(Boolean value)
        {
            XmlDocument doc = GetXmlDoc();
            XmlNode node = doc.SelectSingleNode("/Configs");
            if (node != null)
            {
                XmlAttribute attr = node.Attributes["UpdateCodeEveryTime"];
                if (attr == null) attr = doc.CreateAttribute("UpdateCodeEveryTime");
                attr.InnerText = value.ToString();
                node.Attributes.Append(attr);
                doc.Save(configFilePath);
            }
        }
    }
}
